package com.jingtao.rabbitmq.consumer;

import com.jingtao.rabbitmq.constant.RabbitMQConstants;
import lombok.extern.log4j.Log4j2;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

/**
 * 发布-订阅模式消息消费者配置
 */
@Component
@Log4j2
public class PublisherSubscribeConfig {

    /**
     * 消费者1监听发布-订阅模式消息
     * @param msg 接收到的消息内容
     */
    @RabbitListener(bindings = {
            @QueueBinding(
                    value = @Queue(name = "test_01"),
                    exchange = @Exchange(value = RabbitMQConstants.PUBLISHER_SUBSCRIBE_EXCHANGE_NAME, type = ExchangeTypes.FANOUT)
            )
    })
    public void publisherSubscribeConfig(String msg) {
        log.info("消费者1接收到消息，队列名称：{}，消息的内容：{}", "test_01", msg);
    }

    /**
     * 消费者2监听发布-订阅模式消息
     * @param msg 接收到的消息内容
     */
    @RabbitListener(bindings = {
            @QueueBinding(
                    value = @Queue(name = "test_02"),
                    exchange = @Exchange(value = RabbitMQConstants.PUBLISHER_SUBSCRIBE_EXCHANGE_NAME, type = ExchangeTypes.FANOUT)
            )
    })
    public void publisherSubscribeConfig2(String msg) {
        log.info("消费者2接收到消息，队列名称：{}，消息的内容：{}", "test_02", msg);
    }
}
